package com.holly.unit.deform;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.holly.unit.core.pojo.response.ErrorResponseData;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.deform.api.DeformFacadeApi;
import com.holly.unit.deform.api.exception.DeformException;
import com.holly.unit.deform.api.exception.enums.DeformExceptionEnum;
import com.holly.unit.deform.api.pojo.facade.AddDeformDataRequest;
import com.holly.unit.deform.api.pojo.facade.EditDeformDataRequest;
import com.holly.unit.deform.api.pojo.facade.ListDeformDataRequest;
import com.holly.unit.deform.api.pojo.facade.QueryOrDelDeformDataRequest;
import com.holly.unit.deform.api.pojo.rsp.DeformResult;
import com.holly.unit.deform.context.DeformContext;
import com.holly.unit.deform.entity.DesignFormData;
import com.holly.unit.deform.service.DeformDataService;
import com.holly.unit.query.api.enums.MatchTypeEnum;

import java.util.List;

/**
 * 类描述: 表单设计开放API操作类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/9 14:24
 */
public class DeformFacadeOperator implements DeformFacadeApi{

    private DeformDataService deformDataService = DeformContext.deformDataService();

    @Override
    public ResponseData apiGetData(QueryOrDelDeformDataRequest request) {
        LambdaQueryWrapper<DesignFormData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DesignFormData::getId, request.getDataId());
        queryWrapper.eq(DesignFormData::getDesformCode, request.getDesformCode());
        DesignFormData one = deformDataService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)) {
            throw new DeformException(DeformExceptionEnum.DEFORM_NO_EXIST_ERROR);
        }
        return new SuccessResponseData(JSON.parseObject(one.getDesformDataJson()));
    }

    @Override
    public ResponseData apiAddData(AddDeformDataRequest request) {
        if (request.getFormData() == null) {
            throw new DeformException(DeformExceptionEnum.DEFORM_FACADE_API_ADD_FORMDATA_NULL);
        }
        DesignFormData entity = new DesignFormData();
        entity.setDesformCode(request.getDesformCode());
        entity.setDesformDataJson(request.getFormData().toString());
        DeformResult result = deformDataService.addOne(entity);
        if (result.isSuccess()) {
            return new SuccessResponseData(result);
        } else {
            return new ErrorResponseData(result.getCode().toString(),result.getMessage());
        }
    }

    @Override
    public ResponseData apiEditData(EditDeformDataRequest request) {
        if (request.getFormData() == null) {
            throw new DeformException(DeformExceptionEnum.DEFORM_FACADE_API_ADD_FORMDATA_NULL);
        }
        DesignFormData entity = new DesignFormData();
        entity.setId(request.getDataId());
        entity.setDesformCode(request.getDesformCode());
        entity.setDesformDataJson(request.getFormData().toJSONString());
        DeformResult result = deformDataService.editOne(entity);
        if (result.isSuccess()) {
            return new SuccessResponseData(result);
        } else {
            return new ErrorResponseData(result.getCode().toString(),result.getMessage());
        }

    }

    @Override
    public ResponseData apiDeleteData(QueryOrDelDeformDataRequest request) {
        DesignFormData entity = new DesignFormData();
        entity.setId(request.getDataId());
        entity.setDesformCode(request.getDesformCode());
        DeformResult result = deformDataService.deleteOne(entity, request.getToken());
        if (result.isSuccess()) {
            return new SuccessResponseData(result);
        } else {
            return new ErrorResponseData(result.getCode().toString(),result.getMessage());
        }
    }

    @Override
    public ResponseData apiGetDataList(ListDeformDataRequest request) {

        String desformCode = request.getDesformCode();
        int pageNo = request.getPageNo();
        int pageSize = request.getPageSize();
        MatchTypeEnum matchTypeMenu = MatchTypeEnum.getByValue(request.getMatchType());
        if (matchTypeMenu == null) {
            return new ErrorResponseData("101","matchType只能为'and'或'or'");
        }
        JSONArray queryRulesJSON = JSON.parseArray(request.getQueryRules());
        DeformResult<com.holly.unit.db.api.pojo.page.PageResult<DesignFormData>> rst= deformDataService.pageList(desformCode, pageNo, pageSize, new QueryWrapper<>(), queryRulesJSON, matchTypeMenu);
        List<DesignFormData> rows = rst.getResult().getRows();
        JSONArray formDataArray = new JSONArray(rows.size());
        for (DesignFormData record : rows) {
            JSONObject data = JSON.parseObject(record.getDesformDataJson());
            data.put("id", record.getId());
            data.put("desformId", record.getDesformId());
            data.put("desformCode", record.getDesformCode());
            formDataArray.add(data);
        }
        return new SuccessResponseData(rst.getCode().toString(),rst.getMessage(),formDataArray);
    }
}
